/**
 * Global authority directive
 * Used for fine-grained control of component permissions
 * @Example v-auth="RoleEnum.TEST"
 * !!! : 只能用于简单的按钮。复杂组件不要用这个！
 */
import type { App, Directive, DirectiveBinding } from 'vue'

const authDirective: Directive = {
  mounted: (el: Element, binding: DirectiveBinding<any>) => {
    const {
      uInfo: { authLevel },
    } = useAccount()
    const value = binding.value
    if (!value) return
    if (!value.includes(authLevel)) {
      el.parentNode?.removeChild(el)
    }
  },
}

export function registerVAuth(app: App) {
  app.directive('auth', authDirective)
}

export default authDirective
